/**
 * 
 */
package evance.server.common.utils;

import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;

/**
 * @author BaoVuong
 *
 */
public class MultiPropertiesConfig implements PropertiesConfig,
		InitializingBean {
	private static Logger log = Logger.getLogger(MultiPropertiesConfig.class);
	private java.util.List<Properties> listProperties = new ArrayList<Properties>();
	private String envVariable = "CATALINA_BASE";
	private String configFolder = "conf";
	private Resource[] listPropertyFile;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
	 */
	@Override
	public void afterPropertiesSet() throws Exception {
		try {
			String configHome = System.getenv(envVariable);
			if (configHome != null) {
				Properties pro = new Properties();
				Resource proRes = null;
				for (Resource resource : listPropertyFile) {
					proRes = resource;
					configHome = configHome.replace('\\', '/');
					StringBuilder uriString = new StringBuilder();
					uriString.append("file:");
					uriString.append(configHome);
					uriString.append("/");
					uriString.append(getConfigFolder());
					uriString.append("/");
					uriString.append(resource.getFilename());
					URI uri = new URI(uriString.toString());
					Resource rc = new UrlResource(uri);
					if (rc.exists()) {
						proRes = rc;
					}
					log.info("load property file from: " + proRes.getURI());
					pro.load(proRes.getInputStream());
					listProperties.add(pro);
				}
			} else {
				Properties pro = new Properties();
				for (Resource resource : listPropertyFile) {
					pro.load(resource.getInputStream());
					listProperties.add(pro);
				}
			}

		} catch (IOException e) {
			log.error(e.getMessage(), e);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * evance.server.common.utils.PropertiesConfig#getProperty(java.lang.String)
	 */
	@Override
	public String getProperty(String propertyName) {
		String result = null;
		for (Properties pro : listProperties) {
			result = pro.getProperty(propertyName);
			if (result != null) {
				return result;
			}
		}
		return result;
	}

	/**
	 * @return the listProperties
	 */
	public java.util.List<Properties> getListProperties() {
		return listProperties;
	}

	/**
	 * @param listProperties
	 *            the listProperties to set
	 */
	public void setListProperties(java.util.List<Properties> listProperties) {
		this.listProperties = listProperties;
	}

	/**
	 * @return the envVariable
	 */
	public String getEnvVariable() {
		return envVariable;
	}

	/**
	 * @param envVariable
	 *            the envVariable to set
	 */
	public void setEnvVariable(String envVariable) {
		this.envVariable = envVariable;
	}

	/**
	 * @return the configFolder
	 */
	public String getConfigFolder() {
		return configFolder;
	}

	/**
	 * @param configFolder
	 *            the configFolder to set
	 */
	public void setConfigFolder(String configFolder) {
		this.configFolder = configFolder;
	}

	/**
	 * @return the listPropertyFile
	 */
	public Resource[] getListPropertyFile() {
		return listPropertyFile;
	}

	/**
	 * @param listPropertyFile
	 *            the listPropertyFile to set
	 */
	public void setListPropertyFile(Resource[] listPropertyFile) {
		this.listPropertyFile = listPropertyFile;
	}

}
